
#include "Util.h"
#include <math.h>

#include <fstream>



void eliminateBYShuffle(bool* result, int n, int max)
{
	//error check
	if (max>n)
		max = n; 

	//default is that everyone is not selected
	memset(result, 0, n*sizeof(bool)); 

	//initialize
	int* inds = new int[n]; 
	for(int i=0; i< n; i++)
		inds[i] = i; 

	//shuffle
	for(int i=n-1; i>= n-max; i--)
	{
		int j= intRand(i); 
		int tmp = inds[i]; 
		inds[i] = inds[j]; 
		inds[j] = tmp; 
		result[inds[i]]=true; //the selected item should be turned true
	}

	delete[] inds; 
}


bool fileExists(const char* fileName)
{
  std::fstream fin;
  fin.open(fileName,std::ios::in);
  if( fin.is_open() )
  {
    fin.close();
    return true;
  }
  fin.close();
  return false;
}


//returns a value between 0 and ub (inclusive)
int intRand(int ub)
{
	if(ub==0)
		return 0; 

//	return  rand() / (RAND_MAX / ub);
	return (int) (drand48()*ub); 
}


double floatRand(double ub)
{
	return  (double) (rand() / (double)(RAND_MAX / ub));
//	double result = floatRand(0.0,ub); 
//	printf("rnd: ub: %lf result: %lf\n", ub, result); 
//	return result; 
//	return drand48() * (ub);
}

double floatRand(double lb, double ub)
{
//	return drand48() * (ub-lb) + lb;
	return floatRand(ub-lb) + lb; 
}

double abs_double(double d)
{
	if (d>=0)
		return d; 
	else
		return -d; 
}


double deg2rad(double deg)
{
	return deg/57.3; 
}

double rad2deg(double rad)
{
	return rad*57.3;
}


#ifdef OS_WINDOWS
double drand48(){return floatRand(1.0);}
#endif


#ifndef OS_WINDOWS
timeval measure_time_end, measure_time_start; //used to measure time 

void measureTimeStart()
{
	gettimeofday(&measure_time_start,NULL); 
}

double measureTimeEnd()
{
	gettimeofday(&measure_time_end,NULL); 
	return (double) (measure_time_end.tv_sec - measure_time_start.tv_sec); 
}

#endif






